package com.rabbitmq.seven;

import com.rabbitmq.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: liyanhao
 * @Description: 生产者
 * @Date: 2022/5/11 21:28
 * @Version: v1.0
 */
public class EmitLogTopic {
    //交换机的名称
    public static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        /**
         * 下图绑定关系如下
         * Q1 绑定的是
         *     中间带orange带三个单词的字符串（*.orange.*）
         * Q2  绑定的是
         *     最后一个单词是rabbit的三个单词（*.*.rabbit）
         *     第一个单词是lazy的多个单词(lazy.#)
         */
        Map<String, String> bingdsKeymap = new HashMap<>();
        bingdsKeymap.put("quick.orange.rabbit","被队列 Q1Q2 接收到");
        bingdsKeymap.put("lazy.orange.elephant","被队列 Q1Q2 接收到");
        bingdsKeymap.put("quick.orange.fox","被队列 Q1 接收到");
        bingdsKeymap.put("lazy.brown.fox","被队列 Q2 接收到");
        bingdsKeymap.put("lazy.pink.rabbit","虽然满足两个绑定但只被队列 Q2 接收一次");
        bingdsKeymap.put("quick.brown.fox","不匹配任何绑定不会被任何队列接收到会被丢弃");
        bingdsKeymap.put("quick.orange.male.rabbit","是四个单词不匹配任何绑定会被丢弃");
        bingdsKeymap.put("lazy.orange.male.rabbit","是四个单词但匹配 Q2");
        for (Map.Entry<String, String> bingdsKey : bingdsKeymap.entrySet()) {
            String key = bingdsKey.getKey();
            String message = bingdsKey.getValue();
            channel.basicPublish(EXCHANGE_NAME, key, null, message.getBytes(StandardCharsets.UTF_8));
            System.out.println("生产者发出消息：" + message);
        }


    }

}
